package mn.idax.exchange.otc.web;

import io.swagger.annotations.ApiOperation;
import mn.idax.exchange.auth.bean.AuthInfo;
import mn.idax.exchange.common.SpringUtils;
import mn.idax.exchange.common.bean.ResponseData;
import mn.idax.exchange.otc.dto.RequestDto;
import mn.idax.exchange.otc.dto.ResponseDto;
import mn.idax.exchange.otc.enums.ResponseCodeEnum;
import mn.idax.exchange.otc.exception.IdaxException;
import mn.idax.exchange.otc.service.ChatLogService;
import mn.idax.exchange.otc.util.AuthUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * rest controller for chat
 */
@RestController
public class ChatController {

    private static final Logger logger = LoggerFactory.getLogger(ChatController.class);

    @Autowired
    ChatLogService chatLogService;

    @ApiOperation(value = "otc", notes = "test chat service")
    @RequestMapping(value = "otc", method = {RequestMethod.GET, RequestMethod.POST})
    public String sayHi() {
        return "Hi OTC Chat!";
    }

    @ApiOperation(value = "/getLatestChatLog", notes = "获取指定条数的最新聊天记录<br/>参数类型:Json<br/><br/>[orderId:订单uniqueKey]<br/>[size:记录条数]")
    @RequestMapping(value = "/getLatestChatLog", method = {RequestMethod.POST})
    public ResponseData getLatestChatLog(HttpServletRequest request, @RequestBody RequestDto requestDto) {
        if (StringUtils.isEmpty(requestDto.getOrderId())) {
            return ResponseData.error(ResponseDto.param_error_code, ResponseDto.param_error_msg);
        }

        if (SpringUtils.getBean(ChatLogService.class).checkIsStaffByUserId(AuthUtils.getAuthUserId(request)) ||
                SpringUtils.getBean(ChatLogService.class).isRelatedUser(requestDto.getOrderId(), AuthUtils.getAuthUserId(request))) {
            return ResponseData.success(chatLogService.findLatestChatLog(requestDto.getOrderId(), requestDto.getSize()));
        }
        return ResponseData.error(ResponseDto.auth_failure_code, ResponseDto.auth_failure_msg);
    }

    @ApiOperation(value = "/getEarlierChatLog", notes = "获取指定时间之前的聊天记录<br/>参数类型:Json<br/><br/>[orderId:订单uniqueKey]<br/>[size:记录条数]<br/>time:时间戳UTC")
    @RequestMapping(value = "/getEarlierChatLog", method = {RequestMethod.POST})
    public ResponseData getEarlierChatLog(HttpServletRequest request, @RequestBody RequestDto requestDto) {

        logger.info("----getEarlierChatLog method start,userId is {}", AuthUtils.getAuthUserId(request));
        if (StringUtils.isEmpty(requestDto.getOrderId()) || requestDto.getTime() == null) {
            return ResponseData.error(ResponseDto.param_error_code, ResponseDto.param_error_msg);
        }

        if (SpringUtils.getBean(ChatLogService.class).checkIsStaffByUserId(AuthUtils.getAuthUserId(request)) ||
                SpringUtils.getBean(ChatLogService.class).isRelatedUser(requestDto.getOrderId(), AuthUtils.getAuthUserId(request))) {
            return ResponseData.success(chatLogService.findEarlierChatLog(requestDto.getOrderId(), requestDto.getSize(), requestDto.getTime()));
        }
        return ResponseData.error(ResponseDto.auth_failure_code, ResponseDto.auth_failure_msg);
    }

    @ApiOperation(value = "/getAllChatLog", notes = "获取所有聊天记录<br/>参数类型:Json<br/><br/>[orderId:订单uniqueKey]")
    @RequestMapping(value = "/getAllChatLog", method = {RequestMethod.POST})
    public ResponseData getAllChatLog(HttpServletRequest request, @RequestBody RequestDto requestDto) {

        AuthInfo authInfo = AuthUtils.getAuthInfoByToken(request);
        if (authInfo == null){
            throw new IdaxException(ResponseCodeEnum.USER_EMPTY.getCode());
        }
        logger.info("----getAllChatLog method start,userId is {}", authInfo.getId());
        if (StringUtils.isEmpty(requestDto.getOrderId())) {
            return ResponseData.error(ResponseDto.param_error_code, ResponseDto.param_error_msg);
        }
        boolean isOfficer = authInfo.getSource() == 0;

        if (isOfficer || SpringUtils.getBean(ChatLogService.class).isRelatedUser(requestDto.getOrderId(), authInfo.getId())) {
            return ResponseData.success(chatLogService.findAllChatLog(requestDto.getOrderId(), isOfficer));
        }
        return ResponseData.error(ResponseDto.auth_failure_code, ResponseDto.auth_failure_msg);
    }

}
